using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Ent.Web;
using Ent.Web.Helper;
using Ent.Web.WebControl;
using Ent.Web.WebControl.Parameters;
using Ent.Voice.Show;
public partial class Controls_Event_SetVoting : Ent.Web.WebControl.UserControl
{
    private Settings _pop;
    protected void Page_Load(object sender, EventArgs e)
    {
        bool allowNom = (Values[EventProperty.Participation] != null ? Convert.ToBoolean(Values[EventProperty.Participation].ToString()) : false);
        string nomStartDate = (Values[EventProperty.ParticipationStDate] != null ? Values[EventProperty.ParticipationStDate].ToString() : DateTime.Today.ToShortDateString());
        string evStartDate = (Values[EventProperty.StartDate] != null ? Values[EventProperty.StartDate].ToString() : DateTime.Today.ToShortDateString());
        string evEndDate = (Values[EventProperty.DueDate] != null
            ? Values[EventProperty.DueDate].ToString()
            : DateTime.Parse(evStartDate).AddMonths(Ent.Web.Helper.MathFunction.ConvertToInteger(GetASPXParam("maxDuration"), 2)).ToShortDateString());
        Dictionary<string, string> rep = new Dictionary<string, string>();

        if (nomStartDate != "" && txtVoteStDate.Text == "")
            txtVoteStDate.Text = nomStartDate;

        rvVoteStDate.MaximumValue = evEndDate;
        rvVoteStDate.MinimumValue = nomStartDate;
        rep = new Dictionary<string, string>();
        rep.Add("$1", rvVoteStDate.MinimumValue);
        rep.Add("$2", rvVoteStDate.MaximumValue);
        rep.Add("$3", "Voting Start");
        rvVoteStDate.ErrorMessage = ResourceHelper.Get("MSG_RVDueDate", rep) + " " + ResourceHelper.Get("MSG_NomVote");

        rep = new Dictionary<string, string>();
        rep.Add("$1", "Voting Start Date");
        rfvVoteStDate.ErrorMessage = ResourceHelper.Get("MSG_RFVRequired", rep);

        if (allowNom)
        {
            lblNomStart.Text = ResourceHelper.Get("LBL_NomStartsOn");
            valNomStart.Text = nomStartDate;
        }
        lblEventStart.Text = ResourceHelper.Get("LBL_EventStartsOn"); 
        valEventStart.Text = DateTime.Today.ToShortDateString();
        lblEventEnd.Text = ResourceHelper.Get("LBL_EventEndsOn");
        valEventEnd.Text = evEndDate;
        lblVoting.Text = ResourceHelper.Get("LBL_Voting");
        lblAge.Text = ResourceHelper.Get("LBL_Age");
        lblCountry.Text = ResourceHelper.Get("LBL_Country");
        lblMandatoryField.Text = ResourceHelper.Get("LBL_MandatoryElement");
        lblStartDate.Text = ResourceHelper.Get("LBL_StartDate");
        lblVoterTracking.Text = ResourceHelper.Get("LBL_VoterTracking");
        lblVotingCriteria.Text = ResourceHelper.Get("LBL_VotingCriteria");

        btnShowCal.Attributes.Add("onclick", "show_cal(400, 160, this, '" + txtVoteStDate.ClientID + "');");
        PopuplateControls();
    }
    public override void AddJavascript()
    {
        try
        {
            LoadJavascript("Javascripts/Settings.js");
            LoadJavascript("Javascripts/calendar.js");
            divJavascript.InnerHtml += JavascriptText;
        }
        catch (Exception exc)
        { }
    }
    public void PopuplateControls()
    {
        _pop = new Settings(Server.MapPath("Data/Settings.xml"));
        cblVoterTracking.Items.AddRange(_pop.GetItemCollection("UserTracking"));
        cblMandatory.Items.AddRange(_pop.GetItemCollection("Mandatory"));
        CriteriaHelper ch = Common.GetCriteria("Age");
        foreach (string s in ch.Options)
        {
            ddlAge.Items.Add(s);
        }
        if (ddlAge.Items.Count > 0)
            ddlAge.SelectedIndex = ddlAge.Items.Count - 1;
        ch = Common.GetCriteria("Country");
        foreach (string s in ch.Options)
        {
            ddlCountry.Items.Add(s);
        }
        if (ddlCountry.Items.Count > 0)
            ddlCountry.SelectedIndex = ddlCountry.Items.Count - 1;
    }
    public override void SetEvents() { }
    public override void Init()
    {
        foreach (string key in Values.Keys)
        {
            if (Values[key] == null)
                continue;
            //string noIdkey = key.Replace(ID + "_", "");
            switch (key)
            {
                
                case EventProperty.VoterTracking:
                    SetCollectionControl(cblVoterTracking, (string[])Values[key]);
                    break;
                case EventProperty.Mandatory:
                    SetCollectionControl(cblMandatory, (string[])Values[key]);
                    break;
                case EventProperty.VoteStDate:
                    txtVoteStDate.Text = (string)Values[key];
                    break;
                case EventProperty.Criteria:
                    if (key.Contains(key))
                    {
                        Dictionary<string, string> criteria = (Dictionary<string, string>)Values[key];
                        if (criteria.ContainsKey("Age"))
                            ddlAge.SelectedIndex = ddlAge.Items.IndexOf(ddlAge.Items.FindByValue(criteria["Age"]));
                        if (criteria.ContainsKey("Country"))
                            ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByValue(criteria["Country"]));
                    }
                    break;
                default:
                    break;
            }
        }
    }
    public override void Dispose()
    {
        //throw new Exception("The method or operation is not implemented.");
    }
    public override Dictionary<string, object> GetValues()
    {
        Dictionary<string, object> retValue = new Dictionary<string, object>();
        
        retValue.Add(EventProperty.VoteStDate, txtVoteStDate.Text);
        retValue.Add(EventProperty.Mandatory, Ent.Web.WebControl.Helper.GetSelectedValues(cblMandatory.Items));
        retValue.Add(EventProperty.VoterTracking, Ent.Web.WebControl.Helper.GetSelectedValues(cblVoterTracking.Items));
        Dictionary<string, string> criteria=new Dictionary<string,string>();
        criteria.Add("Age", ddlAge.SelectedValue);
        criteria.Add("Country", ddlCountry.SelectedValue);
        retValue.Add(EventProperty.Criteria, criteria);
        
        return retValue;
    }
}
